﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;
namespace WebApplication.Admin
{
    public partial class PhieuThuAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienthi();
                LoadNhanvien();
                LoadMahdxuat();
            }
        }
        public void LoadHienthi()
        {
            string strid=""+ Request["MaPT"];
            IUnitOfWork iunfitwork = new EFUnitOfWork();
            PHIEUTHURepository repository = new PHIEUTHURepository(new EFRepository<PHIEUTHU>(), iunfitwork);
            if(! string.IsNullOrEmpty(strid))
            {
                PHIEUTHU objPhieuthu = repository.GetById(strid);
                txtmaphieuthu.Value = objPhieuthu.MaPT;
                Dlnhanvien.SelectedValue = objPhieuthu.NHANVIEN.TENNV;
                Dlmahdx.SelectedValue = objPhieuthu.HOADONXUAT.MAHDX;
                txtsotienthu.Value = objPhieuthu.SoTienTra_PT.ToString();
                txtnoidungthu.Value = objPhieuthu.NOIDUNGTHU;
            }
        }
        /// <summary>
        /// Load nhân viên
        /// </summary>
        public void LoadNhanvien()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitfwork);
            IQueryable<NHANVIEN> objNhanvien = repository.All();
            Dlnhanvien.DataSource = objNhanvien;
            Dlnhanvien.DataValueField = "MANV";
            Dlnhanvien.DataTextField = "TENNV";
            Dlnhanvien.DataBind();
            Dlnhanvien.Items.Insert(0, new ListItem {Text="---Chon nhân viên---",Value="0" });
        }
        public void LoadMahdxuat()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            HOADONXUATRepository repository = new HOADONXUATRepository(new EFRepository<HOADONXUAT>(), iunitfwork);
            IQueryable<HOADONXUAT> objHDX = repository.All();
            Dlmahdx.DataSource = objHDX;
            Dlmahdx.DataValueField = "MAHDX";
            Dlmahdx.DataTextField = "MAHDX";
            Dlmahdx.DataBind(); Dlmahdx.Items.Insert(0, new ListItem { Text="---Chọn mã hóa đơn---",Value="0"});
        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            capnhap();
        }
        public void capnhap()
        {
            bool iscapnhap = true;
            string strid = "" + Request["MaPT"];
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            PHIEUTHURepository repository = new PHIEUTHURepository(new EFRepository<PHIEUTHU>(), iunitfwork);
            PHIEUTHU objphieuthu=null;
            if(!string.IsNullOrEmpty(strid))
            {
                objphieuthu=repository.GetById(strid);
                iscapnhap=false;
            }
            else
            {
                objphieuthu=new PHIEUTHU();
                iscapnhap=true;
            }
            if(string.IsNullOrEmpty(txtmaphieuthu.Value) || Dlnhanvien.SelectedValue.Equals("0"))
            {
                RegisterClientScriptBlock("Thông báo","<script> alert('Vui lòng nhập đầy đủ')</script>");
                return;
            }
            objphieuthu.MaPT=txtmaphieuthu.Value;
            objphieuthu.MaNV=Dlnhanvien.SelectedValue;
            objphieuthu.mahdx=Dlmahdx.SelectedValue;
            objphieuthu.SoTienTra_PT=int.Parse(txtsotienthu.Value);
            objphieuthu.NOIDUNGTHU=txtnoidungthu.Value;
            try
            {
                if(iscapnhap)
                {
                    repository.Add(objphieuthu);
                }
                repository.Save();
            }
            catch(Exception ex)
            {
               RegisterClientScriptBlock("Thông báo","<script> alert('Lỗi rồi')</script>");
            }
        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            txtmaphieuthu.Value = "";
                txtnoidungthu.Value="";
                txtsotienthu.Value = "";
                Dlnhanvien.SelectedValue = "0";
                Dlmahdx.SelectedValue = "0";
                Response.Redirect("PhieuThuAdd.aspx");
        }

    }
}